package src.反射3;
/*
使用反射来调用构造器
 */
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class TestUserReflection {
    //1.得到反射类对象
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class<User> userClass = User.class;
        //创建反射类对象
        User user = userClass.newInstance();
        //调用构造器
        Constructor<User> constructor = userClass.getConstructor(String.class);
        User p = constructor.newInstance("pppp");
        System.out.println("p = " + p);

        //调用全部参数的构造器
        Constructor<User> constructor1 = userClass.getConstructor(int.class, String.class);
        User o1 = constructor1.newInstance(50, "王五");
        //关闭访问权限
        constructor1.setAccessible(true);
        System.out.println("o1 = " + o1);
    }
}
